<?php
/**
 * Notes:
 * User: wudg <544038230@qq.com>
 * Date: 2025/9/3 17:14
 */

namespace App\Controller\User;


use App\Controller\ApiController;
use App\Request\User\CardInfoRequest;
use App\Request\User\CardListRequest;
use App\Request\User\CardQrcodeRequest;
use App\Request\User\CardReceiveRequest;
use App\Request\User\CardSaveRequest;
use App\Request\User\UserCardCancellationRequest;
use App\Request\User\UserCardDataRequest;
use App\Request\User\UserCardQrcodeRequest;
use App\Service\Amount\AmountLogService;
use App\Service\User\UserCardService;
use Hyperf\Di\Annotation\Inject;

class CardController extends ApiController
{


    #[Inject]
    protected UserCardService $userCardService;



    #[Inject]
    protected AmountLogService $amountLogService;



    public function cardSave(CardSaveRequest $request)
    {
        $data = $request->validated();

        $cardId = $this->userCardService->cardSave($data);

        return $this->responseOk(['id'=>$cardId]);
    }


    /**
     * 会员卡列表
     * @param CardListRequest $request
     * @return \Psr\Http\Message\ResponseInterface
     */
    public function cardList(CardListRequest $request)
    {
        $data = $request->validated();

        $cardData = $this->userCardService->cardList($data);

        return $this->responseOk($cardData);
    }


    /**
     * 会员卡详细
     * @param CardInfoRequest $request
     * @return \Psr\Http\Message\ResponseInterface
     */
    public function cardInfo(CardInfoRequest $request)
    {
        $data = $request->validated();

        $cardData = $this->userCardService->cardInfo($data['id']);

        return $this->responseOk($cardData);
    }


    /**
     * 生成会员卡领取二维码
     * @param CardQrcodeRequest $request
     * @return \Psr\Http\Message\ResponseInterface
     * @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
     */
    public function cardQrcode(CardQrcodeRequest $request)
    {
        $data = $request->validated();

        $response = $this->userCardService->cardQrcode($data['id'],$data['type'],$data['amount']);
        return $this->responseOk(['qrcode'=>$response]);

    }


    /**
     * 会员领取卡
     * @param CardReceiveRequest $request
     * @return \Psr\Http\Message\ResponseInterface
     */
    public function cardReceive(CardReceiveRequest $request)
    {
        $data = $request->validated();

        $cardInfo = $this->userCardService->cardReceive($data);

        return $this->responseOk($cardInfo);
    }


    /**
     * 用户会员卡数据
     * @param UserCardDataRequest $request
     * @return \Psr\Http\Message\ResponseInterface
     */
    public function userCardData(UserCardDataRequest $request)
    {
        $data = $request->validated();

        $limit = !empty($data['limit']) ? (int)$data['limit'] : 100;
        $cardData = $this->userCardService->userCardData($limit);

        return $this->responseOk($cardData);
    }

    /**
     * 用户会员卡二维码
     * @param UserCardQrcodeRequest $request
     * @return \Psr\Http\Message\ResponseInterface
     */
    public function showUserCardQrCode(UserCardQrcodeRequest $request)
    {
        $data = $request->validated();

        $resQrcode = $this->userCardService->userCardQrcode($data['card_id']);

        return $this->responseOk(['qrcode'=>$resQrcode]);
    }


    /**
     * 用户会员卡核销
     * @param UserCardCancellationRequest $request
     * @return \Psr\Http\Message\ResponseInterface
     */
    public function userCardCancellation(UserCardCancellationRequest $request)
    {
        $data = $request->validated();

        $amountLogs = $this->amountLogService->userCardCancellation($data);
        return $this->responseOk($amountLogs);
    }
}